<template>
  <div class="container">
    <div class="left" />
    <div class="right">
      <div class="header">
        <h2 class="animation a1">青青草原问卷系统</h2>
        <h4 class="animation a2">欢迎使用青青草原问卷系统</h4>
      </div>
      <div class="form">
        <input v-model="loginForm.username" type="username" class="form-field animation a3" placeholder="用户名">
        <input v-model="loginForm.password" type="password" class="form-field animation a4" placeholder="密码">
        <button class="animation a6" @click="handleLogin">登录</button>
      </div>
    </div>
  </div>
</template>

<script>
import md5 from 'js-md5'
import { getSalt } from '@/api/user'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      }
    }
  },
  methods: {
    handleLogin() {
      if (this.loginForm.password === undefined || this.loginForm.password === null || this.loginForm.password === '') {
        this.$message.error('密码不能为空！')
        return
      }
      if (this.loginForm.username === undefined || this.loginForm.username === null || this.loginForm.username === '') {
        this.$message.error('用户名不能为空！')
        return
      }
      const that = this
      getSalt(that.loginForm.username).then(res => {
        if (res.data) {
          that.loginForm.password = md5(that.loginForm.password)
          that.loginForm.password = md5(that.loginForm.password + res.data)
          that.$store.dispatch('user/login', that.loginForm).then(() => {
            that.$router.push({ path: that.redirect || '/' })
            that.loading = false
          }).catch(() => {
            that.loading = false
          }).finally(() => {
            that.loginForm.password = ''
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  font-family: 'Rubik', sans-serif;
  margin:0;
  padding:0;
  display: flex;
  height: 100vh;
}

.left {
  background-image: url(../../../public/images/photo.png);
  background-color: black;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  -webkit-animation-name: left;
          animation-name: left;
  -webkit-animation-duration: 1s;
          animation-duration: 1s;
  -webkit-animation-fill-mode: both;
          animation-fill-mode: both;
  -webkit-animation-delay: 1s;
          animation-delay: 1s;
}

.right {
  flex: 1;
  transition: 1s;
  flex-wrap: wrap;
  flex-direction: column;
  overflow: hidden;
  display: flex;
  justify-content: center;
  margin-left: 28px;
}

.header > h2 {
  margin: 0;
  color: #4f46a5;
}

.header > h4 {
  margin-top: 10px;
  font-weight: normal;
  font-size: 15px;
  color: rgba(0, 0, 0, 0.4);
}

.form {
  max-width: 80%;
  display: flex;
  flex-direction: column;
}

.form > p {
  text-align: right;
}

.form > p > a {
  color: #000;
  font-size: 14px;
}

.form-field {
  height: 46px;
  padding: 0 16px;
  border: 2px solid #ddd;
  border-radius: 4px;
  font-family: 'Rubik', sans-serif;
  outline: 0;
  transition: .2s;
  margin-top: 20px;
}

.form-field:focus {
  border-color: #0f7ef1;
}

.form > button {
  padding: 12px 10px;
  border: 0;
  background: linear-gradient(to right, #de48b5 0%, #0097ff 100%);
  border-radius: 3px;
  margin-top: 10px;
  color: #fff;
  letter-spacing: 1px;
  font-family: 'Rubik', sans-serif;
  cursor:pointer;
}

.animation {
  -webkit-animation-name: move;
          animation-name: move;
  -webkit-animation-duration: .4s;
          animation-duration: .4s;
  -webkit-animation-fill-mode: both;
          animation-fill-mode: both;
  -webkit-animation-delay: 2s;
          animation-delay: 2s;
}

.a1 {
  -webkit-animation-delay: 2s;
          animation-delay: 2s;
}

.a2 {
  -webkit-animation-delay: 2.1s;
          animation-delay: 2.1s;
}

.a3 {
  -webkit-animation-delay: 2.2s;
          animation-delay: 2.2s;
}

.a4 {
  -webkit-animation-delay: 2.3s;
          animation-delay: 2.3s;
}

.a5 {
  -webkit-animation-delay: 2.4s;
          animation-delay: 2.4s;
}

.a6 {
  -webkit-animation-delay: 2.5s;
          animation-delay: 2.5s;
}

@-webkit-keyframes move {
  0% {
    opacity: 0;
    visibility: hidden;
    -webkit-transform: translateY(-40px);
            transform: translateY(-40px);
  }
  100% {
    opacity: 1;
    visibility: visible;
    -webkit-transform: translateY(0);
            transform: translateY(0);
  }
}

@keyframes move {
  0% {
    opacity: 0;
    visibility: hidden;
    -webkit-transform: translateY(-40px);
            transform: translateY(-40px);
  }
  100% {
    opacity: 1;
    visibility: visible;
    -webkit-transform: translateY(0);
            transform: translateY(0);
  }
}
@-webkit-keyframes left {
  0% {
    opacity: 0;
    width: 0;
  }
  100% {
    opacity: 1;
    padding: 20px 40px;
    width: 75%;
  }
}
@keyframes left {
  0% {
    opacity: 0;
    width: 0;
  }
  100% {
    opacity: 1;
    padding: 20px 40px;
    width: 75%;
  }
}
</style>
